/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import basedatos.OperacionesBD;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import modulos.enfymed.modelo.MedicamentoGenerico;
import modulos.usuario.modelo.Usuario;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author medikux
 */
public class iFrmMedicamentos extends javax.swing.JInternalFrame {

    /**
     * Creates new form iFrmMedicamentos
     */
    private List<Object> listMedicamentos = new ArrayList<Object>();
    private Usuario usuario;
    
    public iFrmMedicamentos() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        txtIndicaciones.setEditable(false);
        txtContraindicaciones.setEditable(false);
        listMedicamentos = OperacionesBD.listarPorUsuario("MedicamentoGenerico",usuario);
        cargarMedicamentos();   
        OperacionesVarias.cerrarVentanaEscape(this);
    }
   

    private void cargarMedicamentos() {
        lstMedicamentos.setModel(new AbstractListModel() {

            @Override
            public int getSize() {
                return listMedicamentos.size();
            }

            @Override
            public Object getElementAt(int index) {
                return ((MedicamentoGenerico) listMedicamentos.get(index)).getNombreGenerico();
            }
        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMedicamento = new javax.swing.JTextField();
        pnlId = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtIndicaciones = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtContraindicaciones = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstMedicamentos = new javax.swing.JList();

        setClosable(true);
        setTitle("Medicamentos");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Vial-Pills.png"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 13)); // NOI18N
        jLabel1.setText("Medicamentos:");

        txtMedicamento.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMedicamentoKeyReleased(evt);
            }
        });

        pnlId.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP, null, null));

        txtIndicaciones.setColumns(20);
        txtIndicaciones.setLineWrap(true);
        txtIndicaciones.setRows(5);
        jScrollPane1.setViewportView(txtIndicaciones);

        txtContraindicaciones.setColumns(20);
        txtContraindicaciones.setLineWrap(true);
        txtContraindicaciones.setRows(5);
        jScrollPane2.setViewportView(txtContraindicaciones);

        jLabel5.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel5.setText("Indicaciones");

        jLabel6.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        jLabel6.setText("Contraindicaciones");

        javax.swing.GroupLayout pnlIdLayout = new javax.swing.GroupLayout(pnlId);
        pnlId.setLayout(pnlIdLayout);
        pnlIdLayout.setHorizontalGroup(
            pnlIdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlIdLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 304, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(pnlIdLayout.createSequentialGroup()
                .addGap(130, 130, 130)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(99, 99, 99))
        );
        pnlIdLayout.setVerticalGroup(
            pnlIdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlIdLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlIdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlIdLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addContainerGap())
        );

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lstMedicamentos.setBackground(new java.awt.Color(209, 230, 239));
        lstMedicamentos.setBorder(javax.swing.BorderFactory.createTitledBorder("Medicamentos"));
        lstMedicamentos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstMedicamentosValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(lstMedicamentos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 794, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlId, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtMedicamento, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMedicamento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlId, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtMedicamentoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMedicamentoKeyReleased
        listMedicamentos = OperacionesBD.listarPrediccion("MedicamentoGenerico", "nombreGenerico", txtMedicamento.getText());
        cargarMedicamentos();
        txtContraindicaciones.setText("");
        txtIndicaciones.setText("");
    }//GEN-LAST:event_txtMedicamentoKeyReleased

    private void lstMedicamentosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstMedicamentosValueChanged
        int index = lstMedicamentos.getSelectedIndex();
        if (index != -1) {
            MedicamentoGenerico m = (MedicamentoGenerico) listMedicamentos.get(lstMedicamentos.getSelectedIndex());
            txtContraindicaciones.setText(m.getContraindicaciones());
            txtIndicaciones.setText(m.getIndicaciones());
        }
    }//GEN-LAST:event_lstMedicamentosValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList lstMedicamentos;
    private javax.swing.JPanel pnlId;
    private javax.swing.JTextArea txtContraindicaciones;
    private javax.swing.JTextArea txtIndicaciones;
    private javax.swing.JTextField txtMedicamento;
    // End of variables declaration//GEN-END:variables
}
